<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>编辑角色</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--form表单-->
	<form class="layui-form ok-form" lay-filter="filter">
		<div class="layui-form-item">
			<input type="hidden" id="id" name="id">
			<label class="layui-form-label">角色名</label>
			<div class="layui-input-block">
				<input type="text" name="rolename" placeholder="请输入角色名" autocomplete="off" class="layui-input"
				       lay-verify="required">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-block">
				<input type="text" name="remarks" placeholder="请输入备注" autocomplete="off" class="layui-input"
				       lay-verify="required">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-block">
				<input type="checkbox" name="status" lay-skin="switch" lay-text="启用|停用" checked value="0">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">权限</label>
			<div class="layui-input-block">
				<!-- TODO -->
				<div id="permissionTree"></div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="edit">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script>
	layui.config({
		base: ''
	}).extend({
		authtree: 'authtree',
	});


	layui.use(['element', 'form', 'jquery','okUtils','tree'], function () {
		var element = layui.element;
		var form = layui.form;
		var $ = layui.jquery;
		var okUtils=layui.okUtils;
		var tree=layui.tree;
		okLoading.close();
		let data=[];

        okUtils.ajax("/sysPermission/tree", "post", null, true).done(function (response) {//查看权限树
            data=response.data;
            tree.render({
                elem: "#permissionTree",
                // data: okUtils.mockApi.permission.list,
                data: data,
                showCheckbox: true,
                id: 'permissionId'
            });

            //勾选该角色拥有的权限
            okUtils.ajax("/sysRole/selectPermByRoleId", "post", {id:id}, true).done(function (response) {//查看数据详情
                let perArr=[];
                console.log(response);
                for(var i=0;i<response.data.length;i++){
                    perArr.push(response.data[i].permissionId);

                }
                console.log(perArr);
                tree.setChecked('permissionId', perArr);
            }).fail(function (error) {
                console.log(error);
            });


        }).fail(function (error) {
            console.log(error);
        });

        //获取详情
        let id=okUtils.location.getRequestByKey("id");
        okUtils.ajax("/sysRole/selectById", "post", {id:id}, true).done(function (response) {//查看数据详情
            form.val('filter', {
                "id": response.data.id
                ,"rolename": response.data.rolename
                ,"remarks": response.data.remarks
                ,"status":response.data.status==1?"selected":"false"
            });

        }).fail(function (error) {
            console.log(error);
        });

		form.on('submit(edit)', function (data) {
            var treeData=tree.getChecked('permissionId');
            var permissionIds=okUtils.treeChecked(treeData);
            console.log(permissionIds);
            data.field.id=id;
            data.field.permissionIds=permissionIds;
            // 请求后台
            okUtils.ajax("/sysRole/edit", "post", data.field, true).done(function (response) {
                console.log(response);
                okLayer.greenTickMsg(response.message, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            }).fail(function (error) {
                console.log(error)
            });
            return false;

		});
	})
</script>
</body>
</html>
